// pagesEnterprise/pages/contractInsert/contractInsert.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSubmit: false,
    fileEntity: null,
    formData: {
      agentId: null, // 带看经纪人id 
      agentPhone: null, // 经纪人电话
      rentId: null, // 房产id 
      assessName: null, // 房产名称
      contractPdf: null, // 合同PDF 
      gardenId: null, // 园区id
      money: null, // 签约金额 
      operator: null, // 添加人 
      enterpriseId: null, // 乙方企业id
      partyName: null, // 签约乙方 
      wxUserId: null, // 乙方
      signingCycle: null, // 签约周期
      state: null // 签约 0 未签约 1 已签约 
    }
  },
  /**
   * 上传文件
   */
  async chooseMessageFile() {
    ajax.showLoading()
    const res = await ajax.uploadFilePDF().finally(() => wx.hideLoading())
    this.data.formData.contractPdf = res.url
    this.setData({ fileEntity: res, formData: this.data.formData })
    ajax.showSuccess('上传成功')
    this.isSubmitChange()
  },
  /**
   * 跳转事件
   */
  navigatorEvent(event) {
    const { formData } = this.data
    let url = '';
    const key = event.currentTarget.dataset.key
    if (key == 'agentPhone') url = '/pagesMine/pages/brokerList/brokerList?status=1';
    else if (key == 'partyName') url = `/pagesEnterprise/pages/searchEnterprise/searchEnterprise?gardenId=${formData.gardenId}`;
    else url = `/pagesEnterprise/pages/housingManage/housingManage?status=3&gardenId=${formData.gardenId}`;
    wx.navigateTo({
      url,
      events: {
        backEvent: (res) => {
          if (key == 'agentPhone') (formData.agentPhone = res.phone, formData.agentId = res.id);
          else if (key == 'partyName') (formData.partyName = res.partyName, formData.wxUserId = res.wxUserId, formData.enterpriseId = res.enterpriseId);
          else (formData.assessName = res.housingName, formData.rentId = res.rentId);
          this.setData({ formData })
          this.isSubmitChange()
        }
      }
    })
  },
  /**
   * input 变化事件
   */
  formChange(event) {
    const key = event.currentTarget.dataset.key
    const value = event.detail
    this.setData({ ['formData.' + key]: value })
    this.isSubmitChange()
  },
  /**
   * 校验字段
   */
  isSubmitChange() {
    const { signingCycle, agentId, rentId, contractPdf, money, partyName } = this.data.formData;
    let boo = !(signingCycle && agentId && rentId && contractPdf && money && partyName)
    this.setData({ isSubmit: !boo })
  },
  /**
   * 表单提交
   */
  async onSubmit() {
    const formData = JSON.parse(JSON.stringify(this.data.formData))
    await ajax.post('/wx/contract/add', formData)
    await ajax.showSuccess('操作成功!')
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ 'formData.gardenId': options.gardenId || null })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})